package com.ktkq.action.classroom;



import java.io.File;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.ktkq.action.BaseAction;
import com.ktkq.service.academy.AcademyService;
import com.ktkq.service.classroom.ClassRoomService;
import com.ktkq.util.PageFinder;
import com.ktkq.util.Pageing;
import com.ktkq.util.StringUtil;
import com.ktkq.vo.Academy;
import com.ktkq.vo.CheckIn;
import com.ktkq.vo.ClassRoom;


@SuppressWarnings("serial")
@Controller
public class ClassRoomAction extends BaseAction {
	
	private Pageing pageing;
	
	private PageFinder<ClassRoom> pageFinder;
	private PageFinder<Academy> pageAcademy;
	 private List<File> file;
	 private List<String> fileFileName;
	 private List<String> fileContentType;
  
	
	 @Resource(name="classRoomService") 
	 ClassRoomService classRoomService;
	 @Resource(name="academyService") 
	 AcademyService academyService;
	 
	 ClassRoom classRoom;
	
	int classRoomId;

	int[] deleteList;  //批量删除
    String resultView;    //结果视图       
	@Override
	public String execute() throws Exception {
		if(pageFinder==null){
			pageFinder=new PageFinder<ClassRoom>();
		}
		queryAcademy(); //获取一级下拉框学院
		return INPUT;
	}
	
	/**
	 * 查询分页信息
	 * @return
	 */
	public String findClassRoomList(){
		if(pageing==null){
			pageing=new Pageing();
		}
		if(pageFinder==null){
			pageFinder=new PageFinder<ClassRoom>();
		}
		int page=pageFinder.getPageNo();
		pageFinder.setPageSize(20);
		pageing.setPageSize(20);
		pageing.setStartOfPage(pageFinder.getStartOfPage());
		//分页信息
		pageFinder=classRoomService.queryList(pageing);
		pageFinder.setPageNo(page);
		pageFinder.setPageSize(20);
		pageFinder.refresh();
		queryAcademy(); //获取一级下拉框学院
		return SUCCESS;
	}
	/**
	 * 通过ID查询信息
	 * @return
	 */
	public String findClassRoomById(){
		if(pageFinder==null){
			pageFinder=new PageFinder<ClassRoom>();
		}
			pageFinder.setDataList(classRoomService.queryListById(classRoomId));

			return "edit";
	}
	/**
	 * 添加信息
	 * @return
	 */
	public String addClassRoom(){
//		if(student !=null){
//		studentService.insert(student);
//		}
		if(pageFinder==null){
			pageFinder=new PageFinder<ClassRoom>();
		}
		String uploadFileName  = "studentFile."+StringUtil.getExtention(fileFileName.get(0));
		try {
		if (file != null) {
			
            File savefile = new File(new File(getSavePath()),uploadFileName);
            if (!savefile.getParentFile().exists())
                savefile.getParentFile().mkdirs();
            FileUtils.copyFile(file.get(0), savefile);
			
		}
		} catch (Exception e) {
			  ServletActionContext.getRequest().setAttribute("message","添加失败");
         System.out.println("失败");
		}
		 pageFinder.setDataList(classRoomService.UploadExcel(getSavePath() + "\\" +uploadFileName,classRoom));
			if(pageFinder.getDataList().size()==0){
				  System.out.println("该表没有班级信息");	
				  ServletActionContext.getRequest().setAttribute("message","该表没有班级信息");
			}
			queryAcademy(); //获取一级下拉框学院
		return INPUT;
	}
	
	/**
	 * 修改信息
	 * @return
	 */
	public String updateClassRoom(){
		if(classRoom !=null){
			classRoomService.update(classRoom);
		
		}
		resultView = "/admin/classroom_findClassRoomList.action?pageFinder.pageNo=1";
		return "update";
	}
	/**
	 * 删除信息
	 * @return
	 */
	public String deleteClassRoom(){
		if(deleteList!=null){
		      for(int id:deleteList){
		    	  ClassRoom stu =  new ClassRoom();
		    	  stu.setId(id);
		    	  classRoomService.delete(stu);
		      }
			}else if(classRoom !=null){
			classRoomService.delete(classRoom);
		}
		resultView = "/admin/classroom_findClassRoomList.action?pageFinder.pageNo=1";
		return "update";
	}
	
	/**
	 *获取学院 
	 */
	public void queryAcademy(){
		
			pageing=new Pageing();
		
			pageAcademy=new PageFinder<Academy>();
		
		pageAcademy.setPageSize(20);
		pageing.setPageSize(20);
		pageing.setStartOfPage(pageAcademy.getStartOfPage());
		pageAcademy = academyService.queryList(pageing);
		
	}
	
	/**
	 *获取班级 json
	 */
	public void queryClassRoom(){
		if(pageing==null){
			pageing=new Pageing();	
		}
		pageFinder=new PageFinder<ClassRoom>();
		pageFinder.setPageSize(20);
		pageing.setPageSize(20);
		pageing.setStartOfPage(pageFinder.getStartOfPage());
		pageFinder = classRoomService.queryList(pageing);
		
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			Gson g  = new Gson();
	        String json = g.toJson(new Gson().toJson(pageFinder));
	        PrintWriter pw = response.getWriter();
	        pw.print(json);
	        pw.flush();
	        pw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public Pageing getPageing() {
		return pageing;
	}
	public void setPageing(Pageing pageing) {
		this.pageing = pageing;
	}
	 
	
	 public PageFinder<ClassRoom> getPageFinder() {
		return pageFinder;
	}
	public void setPageFinder(PageFinder<ClassRoom> pageFinder) {
		this.pageFinder = pageFinder;
	}
	public ClassRoomService getClassRoomService() {
		return classRoomService;
	}
	public void setClassRoomService(ClassRoomService classRoomService) {
		this.classRoomService = classRoomService;
	}
	public ClassRoom getClassRoom() {
		return classRoom;
	}
	public void setClassRoom(ClassRoom classRoom) {
		this.classRoom = classRoom;
	}
	public int getClassRoomId() {
		return classRoomId;
	}
	public void setClassRoomId(int classRoomId) {
		this.classRoomId = classRoomId;
	}
	/**
     * 返回上传文件的保存位置
     * 
     * @return
     */
    public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath("/studentFile"); 
    }

 
	public List<File> getFile() {
		return file;
	}
	public void setFile(List<File> file) {
		this.file = file;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}
	public int[] getDeleteList() {
		return deleteList;
	}
	public void setDeleteList(int[] deleteList) {
		this.deleteList = deleteList;
	}
	public String getResultView() {
		return resultView;
	}
	public void setResultView(String resultView) {
		this.resultView = resultView;
	}
	public AcademyService getAcademyService() {
		return academyService;
	}
	public void setAcademyService(AcademyService academyService) {
		this.academyService = academyService;
	}
	public PageFinder<Academy> getPageAcademy() {
		return pageAcademy;
	}
	public void setPageAcademy(PageFinder<Academy> pageAcademy) {
		this.pageAcademy = pageAcademy;
	}

	

	

  
	

}
